import {
  WebSocketGateway as NestWebSocketGateway,
  WebSocketServer,
  SubscribeMessage,
  MessageBody,
} from '@nestjs/websockets';
import { Server } from 'socket.io';
import { WebSocketService } from './websocket.service';

@NestWebSocketGateway({
  cors: {
    origin: '*',
  },
  transports: ['websocket', 'polling'],
})
export class WebSocketGateway {
  @WebSocketServer()
  server: Server;

  constructor(private readonly webSocketService: WebSocketService) {
    // Set the gateway reference in the service to avoid circular dependency
    this.webSocketService.setGateway(this);
  }

  @SubscribeMessage('increment')
  handleIncrement(@MessageBody() data: any) {
    return this.webSocketService.increment();
  }

  @SubscribeMessage('reset')
  handleReset(@MessageBody() data: any) {
    return this.webSocketService.reset();
  }

  @SubscribeMessage('set-interval')
  handleSetInterval(@MessageBody() data: { interval: number }) {
    return this.webSocketService.setInterval(data.interval);
  }
}
